package com.axxc.jpa.entity;

import java.io.Serializable;
import java.util.Comparator;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

import com.axxc.jpa.out.Developer;

import io.swagger.annotations.ApiModel;

@MappedSuperclass
@ApiModel
public class PersonEntity extends Developer
		implements Serializable, Comparable<PersonEntity>, Comparator<PersonEntity> {

	private static final long serialVersionUID = 6072180167947222664L;

	// 升序 Comparable >0 降序<0
	public static final Integer SORT = 0;

	@Id
	@GeneratedValue
	private Long id;

	@Column(columnDefinition = "varchar(25) comment '名称'", nullable = false)
	private String name;

	@Column(length = 25)
	private Integer age;

	@Column(length = 25)
	private Byte sex;

	private String createTime;// 创建时间

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public PersonEntity() {
		super();
	}

	public PersonEntity(Integer age, String name) {
		super();
		this.name = name;
		this.age = age;
	}

	public PersonEntity(Long id, String name, Integer age, Byte sex) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Byte getSex() {
		return sex;
	}

	public void setSex(Byte sex) {
		this.sex = sex;
	}

	// @Override
	// public String toString() {
	// // 高级用法
	// return ToStringBuilder.reflectionToString(this);
	// }

	@Override
	public int compare(PersonEntity p1, PersonEntity p2) {
		// 倒序,若年龄相同 按名称倒序 Comparator
		int desc = p2.getAge().compareTo(p1.getAge());
		if (desc == 0) {
			desc = p2.getName().compareTo(p1.getName());
		}
		return desc;

	}

	@Override
	public int compareTo(PersonEntity p) {
		// 升序 Comparable
		int asc = this.getAge().compareTo(p.getAge());
		if (asc == 0) {
			asc = this.getName().compareTo(p.getName());
		}
		return asc;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((age == null) ? 0 : age.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((sex == null) ? 0 : sex.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PersonEntity other = (PersonEntity) obj;
		if (age == null) {
			if (other.age != null)
				return false;
		} else if (!age.equals(other.age))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (sex == null) {
			if (other.sex != null)
				return false;
		} else if (!sex.equals(other.sex))
			return false;
		return true;
	}

}
